package com.douyuehan.doubao.wechatpay.utils;

import java.util.Random;
import java.util.UUID;

/**
 * Created by warrior on 14/10/22.
 */
public class GeneratorUtil {

    public static String getOrderIdByUUId(){
        int hashCodev = UUID.randomUUID().toString().hashCode();
        if(hashCodev < 0){
            hashCodev = -hashCodev;
        }
        return String.format("%012d", hashCodev);
    }

    public static int verificationCode() {
        int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        Random rand = new Random();
        for (int i = 10; i > 1; i--) {
            int index = rand.nextInt(i);
            int tmp = array[index];
            array[index] = array[i - 1];
            array[i - 1] = tmp;
        }
        int result = 0;
        int i = 0;
        while (result < 100000) {
            result = result * 10 + array[i];
            i++;
        }

        return result;
    }

    /***
     * 生成固定位数数字随机数
     * @param length
     * @return
     */
    public static String generateRandomCode(int length) {
        String base = "0123456789";
        return generateRandomString(length, base);
    }

    /**
     * 获取一定长度的随机字符串
     * @param length 指定字符串长度
     * @return 一定长度的字符串
     */
    public static String getRandomStringByLength(int length) {
        String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        return generateRandomString(length, base);
    }

    /**
     * 获取一定长度的随机字符串
     * @param length 指定字符串长度
     * @return 一定长度的字符串
     */
    public static String generateRandomStringCode(int length) {
        String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        return generateRandomString(length, base);
    }

    public static String generateRandomString(int length, String base) {
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
}
